//
//  HMHomeViewController.swift
//  WymanWeibo
//
//  Created by wyman on 16/4/8.
//  Copyright © 2016年 wyman. All rights reserved.
//

import UIKit

class HMHomeViewController: HMBaseTableViewController {

    private let ReseIdentifier = "SSID"
    //微博模型
    var statuses: [HMStatus]?
    
    override func viewDidLoad() {
        super.viewDidLoad()

        if !HMUserAccountViewModel.shareInstance.userLogin  {
            return
        }
        
        setupNavigationBar()
        
        self.tableView.registerClass(HMStatusCell.self, forCellReuseIdentifier: self.ReseIdentifier)

          //设置预估行高
        tableView.rowHeight = UITableViewAutomaticDimension;
        tableView.estimatedRowHeight = 200

        //加载数据
        HMStatusViewModel.sharedInstance.loadStatus { (statuses, error) in
            if error != nil{
            HMPrint("获取数据失败了:\(error)")
            return
            }

            self.statuses = statuses
//            HMPrint(statuses)
            self.tableView.reloadData()
        }
        
        
    }

   private func setupNavigationBar(){
    
    
    navigationItem.leftBarButtonItem = UIBarButtonItem(imageName: "navigationbar_friendsearch", target: self, action: Selector("didClickPerson:"))
    
     navigationItem.rightBarButtonItem = UIBarButtonItem(imageName: "navigationbar_pop", target: self, action: Selector("didClickQRCode:"))
    
       let title = HMUserAccountViewModel.shareInstance.userAccount?.screen_name ?? "无名"
    //设置title按钮
    let titleBtn = HMHomeTitleButton(title: title)
    
    titleBtn.addTarget(self, action: Selector("didClickTitleBtn:"), forControlEvents: .TouchUpInside)
    
    navigationItem.titleView = titleBtn
    }
    @objc private func didClickTitleBtn(button: HMHomeTitleButton){
        
        HMPrint("点了了home左边里面的按钮")
        
        //点击的话  三角型向上
        button.selected = !button.selected
        HMPrint("\(button.selected)")
        
        UIView.animateWithDuration(HMDefautlAnimDuration) {
            button.imageView?.transform = button.selected ? CGAffineTransformMakeRotation(CGFloat(M_PI)) : CGAffineTransformIdentity
        }
    }
    // MARK: - Table view data source
    @objc private func didClickPerson(button: UIButton){
    
      
    }
    
    @objc private func didClickQRCode(button: UIBarButtonItem){
         HMPrint("点了了home右边里面的按钮")
    }
    
    

}


extension HMHomeViewController{
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.statuses?.count ?? 0
    }

    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCellWithIdentifier(ReseIdentifier,forIndexPath: indexPath ) as! HMStatusCell
//        cell.textLabel?.text = self.statuses![indexPath.row].text
        cell.status = self.statuses![indexPath.row]
        return cell
    }
    
    override func tableView(tableView: UITableView, shouldHighlightRowAtIndexPath indexPath: NSIndexPath) -> Bool {
        return false
    }

}
